package cn.youwan.market;


import cn.youwan.market.messageservice.MessageService;
import cn.youwan.market.utils.AnimCommon;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends ActivityGroup {

	private LinearLayout container;
	public int index;
	private int [] tabs;
	private int [] layoutIds;
	private int [] noSelImage;
	private int [] selImage;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		startService(new Intent(this, MessageService.class));
		
		layoutIds = new int[5];
		
		((ImageView) findViewById(R.id.square_tab))
				.setOnClickListener(mainTabOnClickListener);
		layoutIds[0] = R.id.square_tab;
		
		findViewById(R.id.game_tab).setOnClickListener(
				mainTabOnClickListener);
		layoutIds[1] = R.id.game_tab;
		
		findViewById(R.id.like_tab).setOnClickListener(
				mainTabOnClickListener);
		layoutIds[2] = R.id.like_tab;
		
		findViewById(R.id.software_tab).setOnClickListener(
				mainTabOnClickListener);
		layoutIds[3] = R.id.game_tab;
		
		findViewById(R.id.manage_tab).setOnClickListener(
				mainTabOnClickListener);
		layoutIds[4] = R.id.manage_tab;
		
		
		tabs = new int[5];
		tabs[0] = R.id.square_tab;
		tabs[1] = R.id.game_tab;
		tabs[2] = R.id.like_tab;
		tabs[3] = R.id.software_tab;
		tabs[4] = R.id.manage_tab;
		
		selImage = new int[5];
		selImage[0] = R.drawable.img_tuijain_light;
		selImage[1] = R.drawable.img_download_light;
		selImage[2] = R.drawable.img_download_light;
		selImage[3] = R.drawable.img_installed_light;
		selImage[4] = R.drawable.img_secret_light;
		
		noSelImage = new int[5];
		noSelImage[0] = R.drawable.img_tuijian_gray;
		noSelImage[1] = R.drawable.img_download_gray;
		noSelImage[2] = R.drawable.img_download_gray;
		noSelImage[3] = R.drawable.img_installed_gray;
		noSelImage[4] = R.drawable.img_secret_gray;

		
		container = (LinearLayout) findViewById(R.id.gamecenter_content);
		
		switchMainTabActivity(R.id.square_tab);
	}
	
	public void switchMainTabActivity(int resId) {
		switch (resId) {
		case R.id.square_tab:
			index = 0;
			container.removeAllViews();
			container.addView(
					getLocalActivityManager().startActivity(
							"SquareActivity",
							new Intent(this, SquareActivity.class))
							.getDecorView(), LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			break;
		case R.id.game_tab:
			index = 1;
			container.removeAllViews();
			container.addView(
					getLocalActivityManager().startActivity("GameActivity",
							new Intent(this, GameActivity.class))
							.getDecorView(), LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			break;
		case R.id.like_tab:
			index = 2;
//			container.removeAllViews();
//			container.addView(
//					getLocalActivityManager().startActivity("LikeActivity",
//							new Intent(this, LikeActivity.class))
//							.getDecorView(), LayoutParams.FILL_PARENT,
//					LayoutParams.FILL_PARENT);
			Intent intent=new Intent(MainActivity.this,AppDetailActivity.class);
			AnimCommon.set(R.anim.slide_down_in, R.anim.slide_down_out); 
			startActivity(intent);
			//overridePendingTransition(R.anim.slide_down_in, R.anim.slide_down_out);
			break;
		case R.id.software_tab:
			index = 3;
			container.removeAllViews();
			container.addView(
					getLocalActivityManager().startActivity("SoftwareActivity",
							new Intent(this, SoftwareActivity.class))
							.getDecorView(), LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			break;
		
		case R.id.manage_tab:
			index = 4;
			container.removeAllViews();
			container.addView(
					getLocalActivityManager().startActivity("ManageActivity",
							new Intent(this, ManageActivity.class))
							.getDecorView(), LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT);
			break;
		
		}
		initTabSelected();
	}
	
	OnClickListener mainTabOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int viewId = v.getId();
			switchMainTabActivity(viewId);
		}
	};
	
	private void initTabSelected() {
		for (int i = 0; i < tabs.length; i++) {
			if (i == index) {
				((ImageView) findViewById(tabs[i]))
						.setImageResource(selImage[i]);
			} else {
				((ImageView) findViewById(tabs[i]))
						.setImageResource(noSelImage[i]);
			}
		}
	}
}
